package car.aistar.controller;

import car.aistar.dao.CarDetailDao;
import car.aistar.dao.ReserveDao;
import car.aistar.dto.*;
import car.aistar.entity.*;
import car.aistar.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * (Seller)表控制层
 *
 * @author makejava
 * @since 2020-09-30 20:29:58
 */
/**
*@Author 肖相荣
*@Description //TODO 整个销售人员页面的功能  禁止他人修改增加
*@Date 15:25 2020/10/4
*@Param
*@return
**/
@Controller
@RequestMapping("/seller")
public class SellerController {
    /**
     * 服务对象
     */
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ReserveService reserveService;
    @Autowired
    private UserService userService;
    @Autowired
    private CarDetailService carDetailService;
    @Autowired
    private CarCartService carCartService;
    @Autowired
    private CarownerService carownerService;
    @Autowired
    private XXRxiaoshouService xxRxiaoshouService;
    @Autowired
    private ReserveDao reserveDao;
    @Autowired
    private CarRecordService carRecordService;

    /**
     * 本类用于：跳转到销售员页面，先查询销售员需要的所有数据
     *
     * @author 许华钢
     * @param model
     * @return
     */
    @RequestMapping("/view")
    public String view(Model model,String id,String search,String pageNow,String pageSize) {
        /**
        *@Author 肖相荣
        *@Description //TODO 分页第二版本
        *@Date 21:14 2020/10/4
        *@Param [model, id, pageNow, pageSize]
        *@return java.lang.String
        **/
        pageNow = pageNow==null?"1":pageNow;
        search = search==null?"":search;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "2";
        }
        PageInfo<XXRyuyue> xxRyuyuePageInfo = xxRxiaoshouService.findByPageyuye(Long.valueOf(id),search,Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        model.addAttribute("search",search);
        model.addAttribute("page",xxRyuyuePageInfo);
        return "adCenter/yuyue";


    }
    /**
    *@Author 肖相荣
    *@Description //TODO 销售人员页面的砍价表
    *@Date 15:22 2020/10/4
    *@Param [model, pageNow, pageSize]
    *@return java.lang.String
    **/
    @RequestMapping("/kanjia")
    public String kanjia(Model model,String id,String search,String pageNow,String pageSize) {

        /**
        *@Author 肖相荣
        *@Description //TODO 第二版本
        *@Date 23:03 2020/10/4
        *@Param [model, id, pageNow, pageSize]
        *@return java.lang.String
        **/
        pageNow = pageNow==null?"1":pageNow;
        search = search==null?"":search;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "2";
        }
        PageInfo<Xxrkanjia> xxrkanjiaPageInfo = xxRxiaoshouService.findByPagekanjia(Long.valueOf(id),search,Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        model.addAttribute("search",search);
        model.addAttribute("page",xxrkanjiaPageInfo);
        return "adCenter/Tkanjia";
    }
    /**
    *@Author 肖相荣
    *@Description //TODO 销售人员的负责车辆表
    *@Date 15:22 2020/10/4
    *@Param [model, pageNow, pageSize]
    *@return java.lang.String
    **/
    @RequestMapping("/tcar")
    public String tcar(Model model,String id,String search,String pageNow,String pageSize) {
        pageNow = pageNow==null?"1":pageNow;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "5";
        }
            PageInfo<XxrCarDto> xxrCarDtoPageInfo = xxRxiaoshouService.findByPageCar(Long.valueOf(id),search,Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        model.addAttribute("search",search);
        model.addAttribute("page",xxrCarDtoPageInfo);

        return "adCenter/Tcar";
    }






    /**
    *@Author 肖相荣
    *@Description //TODO 销售页面交易记录表
    *@Date 21:32 2020/10/6
    *@Param
    *@return
    **/
    @RequestMapping("/trecord")
    public String trecord(Model model,String id,String search,String pageNow,String pageSize) {
        pageNow = pageNow==null?"1":pageNow;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "5";
        }
        PageInfo<XxrRecordDto> xxrRecordDtoPageInfo = xxRxiaoshouService.findByPageRecord(Long.valueOf(id),search,Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        model.addAttribute("search",search);
        model.addAttribute("page",xxrRecordDtoPageInfo);
        return "adCenter/Trecord";
    }

    /**
    *@Author 肖相荣
    *@Description //TODO 交易记录表异步删除
    *@Date 21:33 2020/10/6
    *@Param
    *@return
    **/
    @RequestMapping("/trecorddel")
    public @ResponseBody Result recorddel(Model model,String id){

        carRecordService.deleteById(Long.valueOf(id));
//        if (carRecordService.queryById(Long.valueOf(id))!=null){
//            return new Result("400","ok",null);
//        }else {
//            return new Result("200", "ok", null);
//        }
        return new Result("200", "ok", null);
    }






    /**
    *@Author 肖相荣
    *@Description //TODO 销售人员页面的的预约表的异步删除功能
    *@Date 15:23 2020/10/4
    *@Param [model, id]
    *@return car.aistar.dto.Result
    **/


    @RequestMapping("/del")
    public @ResponseBody Result del(Model model,String id){
        reserveService.deleteById(Long.parseLong(id));
//        reserveDao.delByCarId(Long.valueOf(id));


//        xxRxiaoshouService.delByCarId(Long.valueOf(id));
        return new Result("200","ok",null);

    }
    /**
    *@Author 肖相荣
    *@Description //TODO 销售人员的页面的砍价表的异步删除功能
    *@Date 15:24 2020/10/4
    *@Param [model, id]
    *@return car.aistar.dto.Result
    **/
    @RequestMapping("/kanjiadel")
    public @ResponseBody Result kanjiadel(Model model,String id){
        carCartService.deleteById(Long.parseLong(id));

        return new Result("200","ok",null);
    }
    @RequestMapping("/tcardel")
    public @ResponseBody Result tcardel(Model model,String id){

        carDetailService.deleteById(Long.valueOf(id));
        if (carDetailService.queryById(Long.valueOf(id))==null) {
            return new Result("200", "ok", null);
        }else {
            return new Result("400","no",null);
        }
    }




    /**
    *@Author 肖相荣
    *@Description //TODO update
    *@Date 19:41 2020/10/5
    *@Param
    *@return
    **/
    @RequestMapping("/update")
    public @ResponseBody Result kanjiadel(CarDetail carDetail){
        CarDetail carDetail1 = carDetailService.queryById(carDetail.getId());
        if (carDetail.getCStatus()!=null) {
            carDetail1.setCStatus(carDetail.getCStatus());
        }else if(carDetail.getCPrice()!=null){
            carDetail1.setCPrice(carDetail.getCPrice());
        }else  if (carDetail.getCOprice()!=null){
            carDetail1.setCOprice(carDetail.getCOprice());
        }
        carDetailService.update(carDetail1);
//       System.out.println(carDetail1);
        return new Result("200","ok",null);
    }

    /**
    *@Author 肖相荣
    *@Description //TODO 根据预约ID在预约表中批量删除
    *@Date 20:47 2020/10/5
    *@Param
    *@return
    **/
    @RequestMapping("/delallyuyue")
    public @ResponseBody Result delallyuyeu(Model model,String sid){
        String[] sids=sid.split(":");
        if(null!=sids && sids.length>0){
            for (String id : sids) {

                reserveService.deleteById(Long.valueOf(id));

            }
        }
        return new Result("200","ok",null);
    }

    /**
     *@Author 肖相荣
     *@Description //TODO 根据预约ID在预约表中批量删除
     *@Date 20:47 2020/10/5
     *@Param
     *@return
     **/
    @RequestMapping("/delallkanjia")
    public @ResponseBody Result delallkanjia(Model model,String sid){
        String[] sids=sid.split(":");
        if(null!=sids && sids.length>0){
            for (String id : sids) {
                carCartService.deleteById(Long.valueOf(id));
            }
        }
        return new Result("200","ok",null);
    }


    /**
    *@Author 肖相荣
    *@Description //TODO 根据车辆ID在车辆详情表中批量删除负责车辆
    *@Date 20:47 2020/10/5
    *@Param [model, sid]
    *@return car.aistar.dto.Result
    **/
    @RequestMapping("/delallTcar")
    public @ResponseBody Result delallTar(Model model,String sid){
        String[] sids=sid.split(":");
        if(null!=sids && sids.length>0){
            for (String id : sids) {
                carDetailService.deleteById(Long.valueOf(id));
            }
        }
        return new Result("200","ok",null);
    }
    /**
    *@Author 肖相荣
    *@Description //TODO 车辆上下架
    *@Date 21:16 2020/10/5
    *@Param
    *@return
    **/
    @RequestMapping("/add")
    public String  add(){
        System.out.println("ds");
        return "adCenter/Tadd";
    }

    @RequestMapping("/addcar")
    public @ResponseBody Result  addcar(CarDetail carDetail){
        carDetailService.insert(carDetail);
        return new Result("200","ok",null);
    }







}